<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\dao\crud;

use app\dao\BaseDao;
use app\model\crud\QuestionModel;

/**
 * 题目数据访问层
 * Class QuestionDao
 * @package app\dao\crud
 */
class QuestionDao extends BaseDao
{
    /**
     * 设置模型
     * @return string
     */
    protected function setModel(): string
    {
        return QuestionModel::class;
    }

    /**
     * 获取题目列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(array $where, int $page = 0, int $limit = 0, string $field = '*')
    {
        return $this->search($where)->with(['options', 'category'])
            ->when($page && $limit, function ($query) use ($page, $limit) {
                $query->page($page, $limit);
            })
            ->field($field)
            ->order('sort asc, id desc')
            ->select()
            ->toArray();
    }

    /**
     * 获取题目详情（含选项）
     * @param int $id
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getQuestionWithOptions(int $id, string $field = '*')
    {
        return $this->search(['id' => $id, 'is_del' => 0])
            ->with(['options'])
            ->field($field)
            ->find()
            ->toArray();
    }

    /**
     * 根据题目编号获取题目
     * @param string $questionNo
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getByQuestionNo(string $questionNo)
    {
        return $this->search(['question_no' => $questionNo, 'is_del' => 0])
            ->with(['options'])
            ->find()
            ->toArray();
    }

    /**
     * 获取指定类型的题目列表
     * @param int $type
     * @param array $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getListByType(int $type, array $where = [])
    {
        $where['type'] = $type;
        $where['status'] = 1;
        $where['is_del'] = 0;
        
        return $this->search($where)
            ->with(['options'])
            ->order('sort asc, id desc')
            ->select()
            ->toArray();
    }

    /**
     * 批量获取题目（含选项）
     * @param array $ids
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getQuestionsWithOptions(array $ids)
    {
        return $this->getModel()
            ->whereIn('id', $ids)
            ->where('is_del', 0)
            ->with(['options'])
            ->order('sort asc, id desc')
            ->select()
            ->toArray();
    }

    /**
     * 检查题目编号是否存在
     * @param string $questionNo
     * @param int $excludeId
     * @return bool
     */
    public function isQuestionNoExists(string $questionNo, int $excludeId = 0): bool
    {
        $query = $this->getModel()->where('question_no', $questionNo)->where('is_del', 0);
        
        if ($excludeId > 0) {
            $query->where('id', '<>', $excludeId);
        }
        
        return $query->count() > 0;
    }
}

